package com.zx._10_设计模式.创建型模式.单例模式;

/**
 * 单例模式--7. 防止通过反射生成对象
 */
public class Singleton7 {

    private static Singleton7 singleton = new Singleton7();

    private static boolean flag = true;

    private Singleton7() {
        if (flag) {
            flag = !flag;
        } else {
            throw new RuntimeException("禁止反射生成对象");
        }
    }

    public static Singleton7 getInstance() {
        return singleton;
    }

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        Singleton7 instance = Singleton7.getInstance();
        System.out.println(instance);

        try {
            Class<?> clazz = Singleton7.class.getClass();
            Object newInstance = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}